/*******************************************************************************
 * Copyright (c) 2009 Markus Knittig and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Markus Knittig - initial API and implementation
 *******************************************************************************/
package com.googlecode.mylyn.ui.wizard;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.tasks.core.RepositoryTemplate;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import com.googlecode.mylyn.core.GoogleCodeCorePlugin;
import com.googlecode.mylyn.core.util.GoogleCodeUtils;

public class GoogleCodeRepositorySettingsPage extends AbstractRepositorySettingsPage {

    private static final String TITLE = "Google Code Repository Settings";

    private static final String DESCRIPTION = "Enter a Google Code project location (e.g. http://code.google.com/p/googlecode-mylyn-connector/)";

    public GoogleCodeRepositorySettingsPage(TaskRepository taskRepository) {
        super(TITLE, DESCRIPTION, taskRepository);

        setNeedsAdvanced(false);
        setNeedsAnonymousLogin(true);
        setNeedsEncoding(false);
        setNeedsTimeZone(false);
        setNeedsValidation(true);
    }

    @Override
    public void createControl(Composite parent) {
        super.createControl(parent);
        addRepositoryTemplatesToServerUrlCombo();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void createSettingControls(Composite parent) {
        super.createSettingControls(parent);

        // Search for the server label and override the text value
        for (Control control : parent.getChildren()) {
            if (control instanceof Label) {
                Label label = (Label) control;
                if (label.getText().equals(LABEL_SERVER)) {
                    label.setText("Project URL:");
                    return;
                }
            }
        }
    }

    @Override
    protected void createAdditionalControls(Composite parent) {
        // nothing to do
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void createContributionControls(Composite parentControl) {
        // disable contributions to suppress the Task Editor Settings section
    }

    @Override
    public String getConnectorKind() {
        return GoogleCodeCorePlugin.CONNECTOR_KIND;
    }

    @Override
    protected void repositoryTemplateSelected(RepositoryTemplate template) {
        repositoryLabelEditor.setStringValue(template.label);
        setUrl(template.repositoryUrl);
        getContainer().updateButtons();
    }

    @Override
    protected Validator getValidator(TaskRepository repository) {
        return new Validator() {
            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                this.setStatus(Status.OK_STATUS);
                // TODO Check connection for username / password validation
            }
        };
    }

    @Override
    protected boolean isValidUrl(String url) {
        String projectId = GoogleCodeUtils.extractProjectId(url);
        return projectId != null && projectId.length() > 0;
    }

}
